// pages/order/detail/detail.js
import {
  OrderModel
} from '../../../model/order'
import {
  config
} from '../../../config'
var order = new OrderModel()
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    id: 0,
    detail: {},
    address: {},
    bottom: 0,
    cancelTime: 0,
    setInter: null,
    useFlower:0,
    flower:0,
    finalPrice:0,
    isAgent: 0,
    OfflineOrNot:0,
    phone:0,
    wxNickName:''
  },

  sendCode() {
    var cancelTime = this.data.cancelTime

    if (cancelTime > 0) {
      var setInter = setInterval(() => {
        cancelTime--;
        if (cancelTime <= 0) {
          clearInterval(setInter)
          this.setData({
            cancelTime: 0,
            setInter: setInter,
          });
        } else {
          this.setData({
            cancelTime: cancelTime,
          });
        }

      }, 1000);
      this.setData({
        setInter: setInter,
      });
    }
  },

  payAction: function (e) {
    var data = e.currentTarget.dataset
    switch (data.item) {
      case "取消订单":
        this.cancel(data.id)
        break;
      case "拒绝订单":
        this.cancel(data.id)
        break;
      case "立即付款":
        this.pay(data.id)
        break;

      default:
        break;
    }
    console.log(data)
  },

  pay(orderId) {
    order.pay(orderId).then(
      res => {
        if (res.code === 0) {
          console.log(res.data)
          var data = JSON.parse(res.data);
          wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: 'MD5',
            paySign: data.paySign,
            success(resp) {
              wx.showToast({
                title: '支付成功',
                icon: 'none'
              })
              wx.navigateBack()
            },
          })
        }
      }
    )
  },

  cancel(orderId) {
    wx.showModal({
      content: '是否取消订单',
      confirmText: '确认',
      confirmColor: '#F7859C',
      success: res => {
        if (res.confirm) {
          order.cancel(orderId).then(
            res => {
              if (res.code === 0) {
                wx.showToast({
                  title: '订单已取消',
                  icon: 'none'
                })
                wx.navigateBack()
              }
            }
          )
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bottom: config.statusBarHeight,
      isAgent: app.globalData.userInfo.isAgent
    })
    var that = this
    if (options.id) {
      order.detail(options.id).then(
        res => {
          if (res.code === 0) {
            var amount = 0.0
            var count = 0
            res.data.order.cartCommodities.forEach(element => {
              element.cover = element.coverPicture.split(";;")[0]
              count += element.quantity
              if (that.data.isAgent == 1) {
                amount += element.quantity * element.agentPrice
              } else {
                amount += element.quantity * element.retailPrice
              }
            });
            res.data.order.count = count
            res.data.order.amount = amount
            if(amount<=res.data.flower){
              console.log(amount)
              that.setData({
                useFlower:amount
              })
            }else{
              console.log(that.data.useFlower)
              that.setData({
                useFlower:res.data.flower
              })
            }
          }

          this.setData({
            id: options.id,
            detail: res.data.order,
            address: res.data.address,
            cancelTime: res.data.cancelTime,
            OfflineOrNot:res.data.OfflineOrNot,
            phone:res.data.phone,
            wxNickName:res.data.wxNickName,
            flower:res.data.flower,
            finalPrice:res.data.finalPrice
          })
          that.sendCode()
        }
      )
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.setInter) {
      clearInterval(this.data.setInter)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})